package com.ruoyi.project.system.customer.controller

import java.util

import com.ruoyi.project.system.customer.domain.{Customer, IotResponseScala}
import com.ruoyi.project.system.customer.service.ICustomerService
import com.ruoyi.project.tool.{IotResponse, PageResponse, PageResponseScala, ResponseState}
import org.apache.commons.lang3.StringUtils
import org.apache.poi.ss.formula.functions.T
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation._


/**
 * 客户Controller
 *
 * @author daixiang
 * @date 2022-04-15
 */
@RestController
@RequestMapping(Array("/customer"))
class CustomerController @Autowired()(val customerService: ICustomerService) {

  private val log = LoggerFactory.getLogger(classOf[CustomerController])

  /**
   * 查询客户列表
   */
  @PostMapping(Array("/select"))
  def select(@RequestBody request: Customer): IotResponseScala[PageResponseScala[Customer]] = {
    val pageNo = 1
    val pageSize = 10
    if (request.getPageNo == null || (request.getPageNo == 0)) request.setPageNo(pageNo)

    if (request.getPageSize == null || (request.getPageSize == 0)) request.setPageSize(pageSize)
    request.setPageStartNo((request.getPageNo - 1) * request.getPageSize)
    val response = new IotResponseScala[PageResponseScala[Customer]]
    var customerList = new util.ArrayList[Customer]
    var count = 0
    try {
      count = customerService.countAll(request)
      customerList = customerService.selectAll(request)
    } catch {
      case e: Exception =>
        log.error("customer.select异常", e)
        response.setCode(ResponseState.ERROR)
        response.setMsg(e.getMessage)
        return response
    }
    val pageResponse = new PageResponseScala[Customer]
    pageResponse.setList(customerList)
    pageResponse.setPageNo(request.getPageNo)
    pageResponse.setPageSize(request.getPageSize)
    pageResponse.setRows(count)
    pageResponse.setTotalPage(count / request.getPageSize + 1)
    pageResponse.setPageStartNo(request.getPageStartNo)
    response.setCode(ResponseState.SUCCESS)
    response.setMsg("查询成功")
    response.setData(pageResponse)
    response
  }

  /**
   * 新增保存客户
   */
//  @PostMapping(Array("/add"))
//  def addSave(@RequestBody request: Customer): IotResponse[_] = {
//    val response = new IotResponse[Any]
//    response.setCode(ResponseState.SUCCESS)
//    response.setMsg(ResponseState.MSG02)
//    try customerService.insertCustomer(request)
//    catch {
//      case e: Exception =>
//        log.error("customer.add异常", e)
//        response.setCode(ResponseState.ERROR)
//        response.setMsg(ResponseState.MSG03 + e.getMessage)
//        return response
//    }
//    response
//  }

  /**
   * 修改保存客户
   */
//  @PostMapping(Array("/update"))
//  def editSave(@RequestBody request: Customer): IotResponse[_] = {
//    val response = new IotResponse[Any]
//    response.setCode(ResponseState.SUCCESS)
//    response.setMsg(ResponseState.MSG02)
//    try customerService.updateCustomer(request)
//    catch {
//      case e: Exception =>
//        log.error("customer.update异常", e)
//        response.setCode(ResponseState.ERROR)
//        response.setMsg(ResponseState.MSG03 + e.getMessage)
//        return response
//    }
//    response
//  }

  /**
   * 删除客户
   */
//  @GetMapping(Array("/delete"))
//  def remove(ids: String): IotResponse[_] = {
//    val response = new IotResponse[Any]
//    response.setCode(ResponseState.SUCCESS)
//    response.setMsg(ResponseState.MSG02)
//    if (StringUtils.isEmpty(ids)) {
//      response.setCode(ResponseState.ERROR)
//      response.setMsg("请选择要删除的客户")
//      return response
//    }
//    try customerService.deleteCustomerByCustomerNos(ids)
//    catch {
//      case e: Exception =>
//        log.error("customer.delete异常", e)
//        response.setCode(ResponseState.ERROR)
//        response.setMsg(ResponseState.MSG03 + e.getMessage)
//        return response
//    }
//    response
//  }

  /**
   * 查询客户通过id
   */
  @GetMapping(Array("/selectById"))
  def selectById(customerNo: String): IotResponseScala[Customer] = {
    val response = new IotResponseScala[Customer]
    response.setCode(ResponseState.SUCCESS)
    response.setMsg(ResponseState.MSG02)
    try response.setData(customerService.selectCustomerById(customerNo))
    catch {
      case e: Exception =>
        log.error("customer.selectById异常", e)
        response.setCode(ResponseState.ERROR)
        response.setMsg(ResponseState.MSG03 + e.getMessage)
        return response
    }
    response
  }
}
